package org.sunshine.miracle.web.gwt.client;

import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.custom.ThemeSelector;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.ThemeManager;
import com.extjs.gxt.themes.client.Slate;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * User: djiva
 * Date: Dec 9, 2008
 * Time: 4:56:32 PM
 */
public class FileManagerView extends View {

    public static final String CENTER_PANEL = "centerPanel";
    public static final String WEST_PANEL = "westPanel";
    public static final String NORTH_PANEL = "northPanel";

    private Viewport viewport;
    private ContentPanel centerPanel;
    private HtmlContainer northPanel;
    private ContentPanel westPanel;

    public FileManagerView(Controller controller) {
        super(controller);
    }

    protected void handleEvent(AppEvent<?> event) {
        switch (event.type) {
        }
    }


    protected void initialize() {
        viewport = new Viewport();
        viewport.setLayout(new BorderLayout());

        createNorth();
        createWest();
        createCenter();

        RootPanel.get().add(viewport);
    }


    public static TreeModel getTreeModel() {
        BaseTreeModel root = new BaseTreeModel();
        root.set("name", root);

        BaseTreeModel leaf = new BaseTreeModel<TreeModel>(){};
        leaf.set("name", "leaf01");
        root.add(leaf);

        leaf = new BaseTreeModel<TreeModel>();
        leaf.set("name", "leaf02");
        root.add(leaf);

//       Folder root = new Folder("root");
//       for (int i = 0; i < folders.length; i++) {
//         root.add((Folder) folders[i]);
//       }

       return root;
     }


    private void createCenter() {
        centerPanel = new ContentPanel();
        centerPanel.setBorders(false);
        centerPanel.setHeaderVisible(false);
        centerPanel.setLayout(new FitLayout());

        BorderLayoutData data = new BorderLayoutData(Style.LayoutRegion.CENTER);
        data.setMargins(new Margins(5, 5, 5, 0));
        viewport.add(centerPanel, data);
        Registry.register(CENTER_PANEL, centerPanel);
    }

    private void createWest() {
        BorderLayoutData data = new BorderLayoutData(Style.LayoutRegion.WEST, 220, 150, 320);
        data.setMargins(new Margins(5, 5, 5, 5));
        data.setCollapsible(true);
        westPanel = new ContentPanel();
        westPanel.setHeading("File Tree");
        westPanel.setLayout(new FitLayout());
//      ToolBar toolBar = new ToolBar();
//      westPanel.setTopComponent(toolBar);

        FileTreeContainer fileTreeContainer = new FileTreeContainer();
//        Tree tree = new Tree();

        // quick way to build tree from Model instances.
//        TreeBuilder.buildTree(tree, getTreeModel());

        westPanel.add(fileTreeContainer);


        viewport.add(westPanel, data);
        Registry.register(WEST_PANEL, westPanel);
    }

    private void createNorth() {
        StringBuffer sb = new StringBuffer();
        sb.append("<div id='file_manager_header'><div id='theme_container'></div><div id=file_manager_title>Web File Explorer</div></div>");

        northPanel = new HtmlContainer(sb.toString());
        northPanel.setEnableState(false);

        ThemeManager.register(Slate.SLATE);
        ThemeSelector selector = new ThemeSelector();
        selector.setWidth(125);
        northPanel.add(selector, "#theme_container");

        BorderLayoutData data = new BorderLayoutData(Style.LayoutRegion.NORTH, 33);
        data.setMargins(new Margins());
        viewport.add(northPanel, data);
        Registry.register(NORTH_PANEL, northPanel);
    }

}
